package cn.innosmart.aq.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.util.ImageComTools;

/**
 * @De Created by David on 2015/12/7.
 */
public class WorkLogImageAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<String> urls;

    //Size 默认为1  最大Size为9
    private int size = 1;
    private int maxSize = 9;

    /**
     * 日志界面的图片选择适配器
     * @param context
     */
    public WorkLogImageAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.urls = new ArrayList<>();
    }

    public WorkLogImageAdapter(Context context, int size) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.urls = new ArrayList<>();
        this.size = size;
    }

    @Override
    public int getCount() {
        if (size > maxSize) {
            size = 9;
        }
        if (urls.size() > maxSize) {
            ArrayList<String> newUrls = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                newUrls.add(urls.get(i));
            }
            urls = newUrls;
        }
        return urls.size() < size ? urls.size() + 1 : urls.size();
    }

    @Override
    public Object getItem(int position) {
        if (urls.size() < size) {
            if (position >= urls.size())
                return null;
            else
                return urls.get(position);
        } else
            return urls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.gridview_worklog_image, null);
            holder.ivWorkLogPic = (ImageView) convertView.findViewById(R.id.iv_work_log_pic);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (size > urls.size() && position == urls.size()) {
            holder.ivWorkLogPic.setImageResource(R.drawable.share_pic);
        } else {
            Bitmap rawBitmap1 = ImageComTools.scaleBitmap(urls.get(position), 640);
            holder.ivWorkLogPic.setImageBitmap(rawBitmap1);
        }
        return convertView;
    }

    //添加图片
    public void addUrl(String url) {
        urls.add(url);
        notifyDataSetChanged();
    }

    //删除图片
    public void removeUrl(String url) {
        urls.remove(url);
        notifyDataSetChanged();
    }

    public void addUrls(ArrayList<String> urls) {
        this.urls.addAll(urls);
    }

    static class ViewHolder {
        ImageView ivWorkLogPic;
    }
}
